//
//  DKOrderBillsCell.m
//  DanKU
//
//  Created by EDY on 2023/10/30.
//

#import "DKOrderBillsCell.h"
#import "DKOrderRepayViewController.h"
#import "OrderTool.h"

@interface DKOrderBillsCell()

//@property(nonatomic,strong)UIView *dk_totalbilview;
//@property(nonatomic,strong)UILabel *dk_totalbilleft;
//@property(nonatomic,strong)UILabel *dk_totalbilRight;

@property(nonatomic,strong)UIView *dk_OrderCellView;
@property(nonatomic,strong)UIImageView *dk_OrderIcon_BackImg;
@property(nonatomic,strong)UIImageView *dk_OrderIconImg;
@property(nonatomic,strong)UILabel *dk_OrderTitlelab;
@property(nonatomic,strong)UILabel *dk_OrderCountlab;
@property(nonatomic,strong)UIButton *dk_OrderRepayBtn;
@property(nonatomic,strong)UILabel *dk_OrderCountStatelab;
@property(nonatomic,strong)UILabel *dk_OrderDatelab;

//@property(nonatomic,strong)UILabel *dk_OrderBilsFoot;

@end

@implementation DKOrderBillsCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.backgroundColor =  DK_Hex(@"#FAFAFA");
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self creatUI];
    }
    return  self;
}

-(void)setDateSouce:(DKOrderRecordListModel *)dateSouce{
    _dateSouce = dateSouce;
    self.dk_OrderTitlelab.text = dateSouce.productName;
    
    self.dk_OrderDatelab.textColor = DK_Hex(@"#999999");

    DKRepaymentDetail *model = dateSouce.repaymentDetail;
    self.dk_OrderCountlab.text = [NSString stringWithFormat:@"-%@",[OrderTool formatAmountForIndia:model.repaymentAmount]];
    
    
    NSString  *formattedDateString = [OrderTool convertDate:[OrderTool stringFromTimestampString:model.repaymentTime] fromFormat:@"yyyy-MM-dd HH:mm:ss" toFormat:@"dd-MM-yyyy"];
    
    self.dk_OrderDatelab.text = formattedDateString;
    if (dateSouce.status == 180) {
        self.dk_OrderDatelab.textColor = DK_Hex(@"#FF0000");
    }
    
//    
//    DKUserBaseInfoModel *dk_UserBaseInfoMdl = [DKGlobalHelper defaultManager].userBaseInfoMdl;
//    self.dk_totalbilRight.text =  [NSString stringWithFormat:@"-%@",[OrderTool formatAmountForIndia:dk_UserBaseInfoMdl.repaymentTotalAmount]];
//    
}


//-(void)setIsTotal:(BOOL)isTotal{
////    self.dk_totalbilview.hidden = YES;
//    
//    self.dk_totalbilview.hidden = !isTotal;
//    
//    if (isTotal) {
//        self.dk_totalbilview.hidden = NO;
//        [self.dk_OrderCellView mas_updateConstraints:^(MASConstraintMaker *make) {
//            make.top.equalTo(self.contentView).offset(50);
//        }];
//    }else{
//        self.dk_totalbilview.hidden = YES;
//    }
//    
//  
//}

//-(void)setType:(NSInteger)type{
//    if (type == 1) {
//        self.dk_OrderBilsFoot.hidden = NO;
//    }else{
//        self.dk_OrderBilsFoot.hidden = YES;
//    }
//}

-(void)creatUI{
    
//    [self.contentView addSubview:self.dk_totalbilview];
//    [self.dk_totalbilview addSubview:self.dk_totalbilleft];
//    [self.dk_totalbilview addSubview:self.dk_totalbilRight];
//    
//    [self.dk_totalbilview mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(self.contentView).offset(20);
//        make.right.equalTo(self.contentView).offset(-20);
//        make.top.equalTo(self.contentView);
//        make.height.mas_equalTo(50);
//    }];
//    
//    [self.dk_totalbilleft mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.top.bottom.equalTo(self.dk_totalbilview);
//        make.width.mas_equalTo(DK_SCREEN_W/3);
//    }];
//    
//    [self.dk_totalbilRight mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.right.top.bottom.equalTo(self.dk_totalbilview);
//        make.width.mas_equalTo(DK_SCREEN_W/3);
//    }];
    
    [self.contentView addSubview:self.dk_OrderCellView];
    [self.dk_OrderCellView addSubview:self.dk_OrderIcon_BackImg];
    [self.dk_OrderCellView addSubview:self.dk_OrderIconImg];
    [self.dk_OrderCellView addSubview:self.dk_OrderTitlelab];
    [self.dk_OrderCellView addSubview:self.dk_OrderCountlab];
    [self.dk_OrderCellView addSubview:self.dk_OrderRepayBtn];
    [self.dk_OrderCellView addSubview:self.dk_OrderCountStatelab];
    [self.dk_OrderCellView addSubview:self.dk_OrderDatelab];
    
    
    [self.dk_OrderCellView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(15);
        make.right.equalTo(self.contentView).offset(-15);
        make.top.equalTo(self.contentView).offset(5);
        make.bottom.equalTo(self.contentView).offset(-5);
    }];
    
    [self.dk_OrderIcon_BackImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.dk_OrderCellView).offset(15);
        make.width.height.mas_equalTo(38);
        make.top.equalTo(self.dk_OrderCellView).offset(21);
    }];
    
    [self.dk_OrderIconImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(22);
        make.height.mas_equalTo(26);
        make.center.equalTo(self.dk_OrderIcon_BackImg);
    }];
    
    [self.dk_OrderTitlelab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.dk_OrderIcon_BackImg.mas_right).offset(10);
        make.width.mas_equalTo(DK_SCREEN_W/3.8);
        make.height.mas_equalTo(30);
        make.top.equalTo(self.dk_OrderCellView).offset(10);
    }];
    
    [self.dk_OrderCountlab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.dk_OrderTitlelab.mas_right);
        make.width.height.top.equalTo(self.dk_OrderTitlelab);
    }];
    
    [self.dk_OrderRepayBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.dk_OrderCellView.mas_right).offset(-15);
        make.width.mas_equalTo(60);
        make.height.mas_equalTo(25);
        make.centerY.equalTo(self.dk_OrderIcon_BackImg);
    }];
    
    [self.dk_OrderCountStatelab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.left.equalTo(self.dk_OrderTitlelab);
        make.top.equalTo(self.dk_OrderTitlelab.mas_bottom);
        make.width.mas_equalTo(DK_SCREEN_W/5);
    }];
    
    [self.dk_OrderDatelab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.dk_OrderCountStatelab.mas_right);
        make.height.top.equalTo(self.dk_OrderCountStatelab);
        make.right.equalTo(self.dk_OrderCountlab);
    }];
    
//    [self.dk_OrderBilsFoot removeFromSuperview];
//    [self.dk_OrderCellView addSubview:self.dk_OrderBilsFoot];
//    [self.dk_OrderBilsFoot mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(self.dk_OrderIcon_BackImg);
//        make.width.equalTo(self.dk_OrderTitlelab);
//        make.top.equalTo(self.dk_OrderDatelab.mas_bottom);
//        make.bottom.equalTo(self.dk_OrderCellView);
//    }];
    
    // 更新布局
       [self setNeedsLayout];
       [self layoutIfNeeded];
}



#pragma mark - 详情
-(void)listinfo{
    DKOrderRepayViewController *repay = [DKOrderRepayViewController new];
//    repay.orderNo = self.dateSouce.orderNo;
    repay.dateSouce = self.dateSouce;
    [[self viewController].navigationController pushViewController:repay animated:YES];
}

#pragma mark - 懒加载
//-(UIView*)dk_totalbilview{
//    if (!_dk_totalbilview) {
//        _dk_totalbilview = [[UIView alloc]init];
//        _dk_totalbilview.hidden = YES;
//    }
//    return _dk_totalbilview;
//}
//
//- (UILabel *)dk_totalbilleft {
//    if (!_dk_totalbilleft) {
//        _dk_totalbilleft = [[UILabel alloc] init];
//        _dk_totalbilleft.textColor = DK_Hex(@"#333333");
//        _dk_totalbilleft.font = [UIFont boldSystemFontOfSize:16];
//        _dk_totalbilleft.text = @"Total Bills";
//    }
//    return _dk_totalbilleft;
//}
//
//
//- (UILabel *)dk_totalbilRight {
//    if (!_dk_totalbilRight) {
//        _dk_totalbilRight = [[UILabel alloc] init];
//        _dk_totalbilRight.textColor = DK_Hex(@"#333333");
//        _dk_totalbilRight.font = [UIFont systemFontOfSize:20];
//        _dk_totalbilRight.text = @"-₹ 10,000";
//        _dk_totalbilRight.textAlignment = 2;
//    }
//    return _dk_totalbilRight;
//}



#pragma mark - 懒加载
-(UIView*)dk_OrderCellView{
    if (!_dk_OrderCellView) {
        _dk_OrderCellView = [[UIView alloc]init];
        _dk_OrderCellView.backgroundColor = DK_Hex(@"#FFFFFF");
        _dk_OrderCellView.layer.cornerRadius = 10;
    }
    return _dk_OrderCellView;
}

- (UIImageView*)dk_OrderIcon_BackImg{
    if (!_dk_OrderIcon_BackImg) {
        _dk_OrderIcon_BackImg = [[UIImageView alloc] init];
        _dk_OrderIcon_BackImg.image =  DK_GetImage(@"dk_order_list_back_img");
    }
    return _dk_OrderIcon_BackImg;
}

- (UIImageView*)dk_OrderIconImg{
    if (!_dk_OrderIconImg) {
        _dk_OrderIconImg = [[UIImageView alloc] init];
        _dk_OrderIconImg.image =  DK_GetImage(@"dk_order_list_img");
    }
    return _dk_OrderIconImg;
}

- (UILabel *)dk_OrderTitlelab {
    if (!_dk_OrderTitlelab) {
        _dk_OrderTitlelab = [[UILabel alloc] init];
        _dk_OrderTitlelab.textColor = DK_Hex(@"#333333");
        _dk_OrderTitlelab.font = [UIFont boldSystemFontOfSize:15];
        _dk_OrderTitlelab.text = @"Go 002";
    }
    return _dk_OrderTitlelab;
}

- (UILabel *)dk_OrderCountlab {
    if (!_dk_OrderCountlab) {
        _dk_OrderCountlab = [[UILabel alloc] init];
        _dk_OrderCountlab.textColor = DK_Hex(@"#333333");
        _dk_OrderCountlab.font = [UIFont boldSystemFontOfSize:15];
        _dk_OrderCountlab.text = @"₹3,000";
        _dk_OrderCountlab.textAlignment = 2;
    }
    return _dk_OrderCountlab;
}

-(UIButton*)dk_OrderRepayBtn{
    if (!_dk_OrderRepayBtn) {
        _dk_OrderRepayBtn = [[UIButton alloc]init];
        [_dk_OrderRepayBtn addTarget:self action:@selector(listinfo) forControlEvents:UIControlEventTouchUpInside];
        [_dk_OrderRepayBtn setBackgroundImage:[UIImage imageNamed:@"Order_rectangel_button"] forState:UIControlStateNormal];
        [_dk_OrderRepayBtn setBackgroundImage:[UIImage imageNamed:@"Order_rectangel_button"] forState:UIControlStateSelected];
        [_dk_OrderRepayBtn setTitle:@"Repay" forState:UIControlStateNormal];
        _dk_OrderRepayBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        [_dk_OrderRepayBtn setTitleColor:DK_Hex(@"#FFFFFF") forState:UIControlStateNormal];
        _dk_OrderRepayBtn.layer.cornerRadius = 5;
        _dk_OrderRepayBtn.layer.maskedCorners = YES;
        
        }
    return _dk_OrderRepayBtn;
}

- (UILabel *)dk_OrderCountStatelab {
    if (!_dk_OrderCountStatelab) {
        _dk_OrderCountStatelab = [[UILabel alloc] init];
        _dk_OrderCountStatelab.textColor = DK_Hex(@"#999999");
        _dk_OrderCountStatelab.font = [UIFont boldSystemFontOfSize:13];
        _dk_OrderCountStatelab.text = @"Due date";
    }
    return _dk_OrderCountStatelab;
}

- (UILabel *)dk_OrderDatelab {
    if (!_dk_OrderDatelab) {
        _dk_OrderDatelab = [[UILabel alloc] init];
        _dk_OrderDatelab.textColor = DK_Hex(@"#999999");
        _dk_OrderDatelab.font = [UIFont boldSystemFontOfSize:13];
        _dk_OrderDatelab.textAlignment = 2;
        _dk_OrderDatelab.text = @"18-07-2022";
    }
    return _dk_OrderDatelab;
}

//- (UILabel *)dk_OrderBilsFoot {
//    if (!_dk_OrderBilsFoot) {
//        _dk_OrderBilsFoot = [[UILabel alloc] init];
//        _dk_OrderBilsFoot.textColor = DK_Hex(@"#FF0000");
//        _dk_OrderBilsFoot.font = [UIFont boldSystemFontOfSize:11];
//        _dk_OrderBilsFoot.text = @"Partial repayment";
//        _dk_OrderBilsFoot.hidden = YES;
//    }
//    return _dk_OrderBilsFoot;
//}

//获取View所在的Viewcontroller方法
- (UIViewController *)viewController {
    for (UIView* next = [self superview]; next; next = next.superview) {
        UIResponder *nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            return (UIViewController *)nextResponder;
        }
    }
    return nil;
}


@end
